package com.unbook.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unbook.entity.ResponseVo;
import com.unbook.entity.School;
import com.unbook.entity.params.SchoolUpdateBody;
import com.unbook.service.ISchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/school")
public class SchoolController {
    @Autowired
    ISchoolService schoolService;

    @GetMapping("/list")
    public ResponseVo<List<School>> list(@RequestParam(required = false,defaultValue = "")String name,@RequestParam(required = false,defaultValue = "")String province,@RequestParam(required = false,defaultValue = "")String city){
        return ResponseVo.success(schoolService.queryList(name, province, city));
    }
    @GetMapping("/pageList")
    public ResponseVo<IPage<School>> listPage(
        @RequestParam Long pageNum,
        @RequestParam Long pageSize,
        @RequestParam(required = false) String name,
        @RequestParam(required = false,defaultValue = "")String province,
        @RequestParam(required = false,defaultValue = "")String city
    ){
        LambdaQueryWrapper<School> queryWrapper = new QueryWrapper<School>().lambda();
        if(name != null)
            queryWrapper.like(School::getName, name);
        if(province != null)
            queryWrapper.like(School::getProvince, province);
        if(city != null)
            queryWrapper.like(School::getCity, city);
        return ResponseVo.success(schoolService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }

    @PostMapping("/insert")
    public ResponseVo<Boolean> insert(@RequestBody School body){
        return ResponseVo.success(schoolService.save(body));
    }
    @PutMapping("/update")
    public ResponseVo<Boolean> update(@RequestBody SchoolUpdateBody body){
        School school = School.builder()
            .sId(body.getSId())
            .name(body.getName())
            .type(body.getType())
            .province(body.getProvince())
            .city(body.getCity())
            .build();
        return ResponseVo.success(schoolService.updateById(school));
    }
    @DeleteMapping("/delete")
    public ResponseVo<Boolean> delete(@RequestParam String sIds){
        List<String> appIdList = Stream.of(sIds.split(",")).collect(Collectors.toList());
        return schoolService.removeByIds(appIdList) ? ResponseVo.success(null) : ResponseVo.deleteErr();
    }
    @PostMapping("/import")
    public ResponseVo<Boolean> importSchools(@RequestParam("file")MultipartFile file){
        return schoolService.saveBatch(schoolService.importSchool(file), 2000) ? ResponseVo.success() : ResponseVo.error("导入失败");
    }
}
